<?php
namespace app\task\controller;
use app\BaseController;
use app\common\model\mysql\YwCountdowmConfig;
use app\common\model\mysql\YwCountdowmRecord;
use app\common\model\mysql\YwCoupon;
use app\common\model\mysql\YwCouponReceiveRecord;
use think\facade\Log;

class Task extends BaseController
{

    public function initialize()
    {
    }

    /*
     * 检查卡券的过期
     */
    public function checkCouponExpire(){
        Log::write('检查卡券过期开始:'.date('Y-m-d H:i:s'));
        $where = [];
        $where[] = ['is_expire','=',0];
        $where[] = ['end_time','<',date('Y-m-d H:i:s')];
        $lists = YwCoupon::where($where)->field('id')->select();
        if(!empty($lists)){
            foreach ($lists as $item){
                YwCoupon::where(['id'=>$item['id']])->update(['is_expire'=>1]);
                YwCouponReceiveRecord::where(['coupon_id'=>$item['id'],'status'=>1])->update(['status'=>4]);
            }
        }
        Log::write('检查卡券过期结束:'.date('Y-m-d H:i:s'));
        echo 'done';
        exit();
    }

    /*
     * 检查倒计时(每天凌晨生成一天的倒计时数据)
     */
    public function checkCountdown(){
        set_time_limit(0);
        Log::write('检查倒计时开始:'.date('Y-m-d H:i:s'));
        $lists = YwCountdowmConfig::where(['is_delete'=>0,'status'=>1])->order('start_time','asc')->select();
        if(!empty($lists)){
            $number_t = 1;
            $insertDataAll = [];
            foreach ($lists as $item){
                if($item['prepare_time_num'] > 0 && $item['sustain_time_num'] > 0){
                    $is_circle = true;
                    $number = 1;
                    while ($is_circle){
                        $start_time_t = strtotime(date('Y-m-d').' '.$item['start_time']) + $number * $item['prepare_time_num'] +
                            ($number - 1) * $item['sustain_time_num'];
                        $end_time_t = strtotime(date('Y-m-d').' '.$item['start_time']) +
                            $number * $item['prepare_time_num'] + $number_t * $item['sustain_time_num'];
                        $start_time = date('Y-m-d H:i:s',$start_time_t);
                        $end_time = date('Y-m-d H:i:s',$end_time_t);
                        if($end_time < date('Y-m-d').' '.$item['end_time']){
                            $insertData = [
                                'start_time' => $start_time,
                                'end_time' => $end_time,
                                'number' => date('Ymd').$this->getNumber($number_t),
                                'create_time' => date('Y-m-d H:i:S')
                            ];
                            $insertDataAll[] = $insertData;
                            $number_t ++;
                            $number ++;
                        }else{
                            $is_circle = false;
                        }
                    }
                }else{
                    $insertData = [
                        'start_time' => $item['start_time'],
                        'end_time' => $item['end_time'],
                        'number' => date('Ymd').$this->getNumber($number_t),
                        'create_time' => date('Y-m-d H:i:S')
                    ];
                    $insertDataAll[] = $insertData;
                    $number_t++;
                }
            }
            YwCountdowmRecord::insertAll($insertDataAll);
        }
        Log::write('检查倒计时结束:'.date('Y-m-d H:i:s'));
        echo 'done';
        exit();
    }

    public function getNumber($number){
        $len = strlen($number);
        switch ($len){
            case 1:
                return '00'.$number;
            case 2:
                return '0'.$number;
            default:
                return $number;
        }
    }

}
